// 获取商品id
let pid = "";
let searchStr = location.search;
let searchArr = searchStr.split("&");
// console.log(searchArr);
searchArr.forEach(function (item, index) {
  //   item.split("=");
  pid = searchArr[0].split("=")[1];
});
console.log(pid); //获取到了pid
let a;
//获取商品详情
let main = document.querySelector("main .in");
async function shopdetail(pid) {
  let shopDetailAPI = "http://jx.xuzhixiang.top/ap/api/detail.php";
  let params = { id: pid };
  let { data } = await axios.get(shopDetailAPI, { params }); //结构赋值
  console.log(data);
  let obj = data.data; //获取到数据
  //   uid = obj.uid;
  let str = ` <img
  src="${obj.pimg}"
  alt=""/>
  <div>
   <p class="name">${obj.pname}</p>
   <p class="desc"><span>【直降130元】</span>${obj.pdesc}(${obj.pid})</p>
   <p class="time flex">
    <svg t="1633591163990" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5162" width="18" height="18"><path d="M384 256h-42.666667v-42.666667a170.666667 170.666667 0 1 1 341.333334 0v42.666667h-42.666667v-42.666667c0-70.58-57.42-128-128-128s-128 57.42-128 128z m550 665.386667l-77.333333-618.666667a53.42 53.42 0 0 0-52.913334-46.72H682.666667v133.74a42.666667 42.666667 0 1 1-42.666667 0V256H384v133.74a42.666667 42.666667 0 1 1-42.666667 0V256H220.246667A53.42 53.42 0 0 0 167.333333 302.72l-77.333333 618.666667a53.333333 53.333333 0 0 0 52.913333 59.946666h738.173334a53.333333 53.333333 0 0 0 52.92-59.946666z" fill="#ffffff" p-id="5163"></path></svg>
    <span>秒杀中</span>
    <span>距离结束<i>00</i>天
      <a>00</a>时<a>00</a>分<a>00</a>秒
    </span>
    <span>更多活动
      <span class="iconfont icon-xiangyoujiantou"></span>
    </span>
    </p>
    <div class="price">
      <div>￥${obj.pprice}</div>
      <div>
        <p><a>定制</a>免费定制服务</p>
        <p><a>积分</a>购物送积分</p>
      </div>
    </div>
    <p class="support"><span>商品支持:</span>
    <svg t="1633594279891" class="icon" viewBox="0 0 1056 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="10906" width="20" height="20"><path d="M521.28 96.128c229.696 0 415.872 186.176 415.872 415.872 0 229.696-186.176 415.872-415.872 415.872-229.696 0-415.872-186.176-415.872-415.872 0-229.696 186.176-415.872 415.872-415.872z m0 64C326.944 160.128 169.408 317.664 169.408 512s157.536 351.872 351.872 351.872S873.152 706.336 873.152 512 715.616 160.128 521.28 160.128z m199.168 234.496a32 32 0 0 1 1.28 42.304l-2.752 2.944-232.64 217.984a32 32 0 0 1-41.728 1.728l-2.944-2.624-119.008-120.768a32 32 0 0 1 42.592-47.616l3.008 2.688 97.088 98.528 209.856-196.608a32 32 0 0 1 45.248 1.44z" p-id="10907" fill="#d81e06"></path></svg>
    花呗分期
    <svg t="1633594279891" class="icon" viewBox="0 0 1056 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="10906" width="20" height="20"><path d="M521.28 96.128c229.696 0 415.872 186.176 415.872 415.872 0 229.696-186.176 415.872-415.872 415.872-229.696 0-415.872-186.176-415.872-415.872 0-229.696 186.176-415.872 415.872-415.872z m0 64C326.944 160.128 169.408 317.664 169.408 512s157.536 351.872 351.872 351.872S873.152 706.336 873.152 512 715.616 160.128 521.28 160.128z m199.168 234.496a32 32 0 0 1 1.28 42.304l-2.752 2.944-232.64 217.984a32 32 0 0 1-41.728 1.728l-2.944-2.624-119.008-120.768a32 32 0 0 1 42.592-47.616l3.008 2.688 97.088 98.528 209.856-196.608a32 32 0 0 1 45.248 1.44z" p-id="10907" fill="#d81e06"></path></svg>
    以旧换新
    <svg t="1633594279891" class="icon" viewBox="0 0 1056 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="10906" width="20" height="20"><path d="M521.28 96.128c229.696 0 415.872 186.176 415.872 415.872 0 229.696-186.176 415.872-415.872 415.872-229.696 0-415.872-186.176-415.872-415.872 0-229.696 186.176-415.872 415.872-415.872z m0 64C326.944 160.128 169.408 317.664 169.408 512s157.536 351.872 351.872 351.872S873.152 706.336 873.152 512 715.616 160.128 521.28 160.128z m199.168 234.496a32 32 0 0 1 1.28 42.304l-2.752 2.944-232.64 217.984a32 32 0 0 1-41.728 1.728l-2.944-2.624-119.008-120.768a32 32 0 0 1 42.592-47.616l3.008 2.688 97.088 98.528 209.856-196.608a32 32 0 0 1 45.248 1.44z" p-id="10907" fill="#d81e06"></path></svg>
    积分抵现
    </p>
    <div class="num flex">
      <p>数量：</p>
      <p class="flex">
        <a id="sub">-</a>
        <input type="number" name="" min="1" value="1" id="iptNum"/>
        <a id="add">+</a>
      </p>  
    </div>
    <p class="priceSum">
       <span>
          ￥<a>${obj.pprice}</a>
       </span>
       <span>
       已选：
       <a>${obj.pname}</a>
       </span>
    </p>
    <input type="button" value="加入购物车" id="btn1" onclick="addShop()"/>
    <input type="button" value="立即购买" id="btn2"/>
    </div>`; //拼接字符串
  main.innerHTML = str;
  let priceSum = document.querySelector(".priceSum span:first-child a");
  // console.log(priceSum);
  let price = obj.pprice;
  // console.log(price);
  //改变输入框里的数量
  document.querySelector("#sub").onclick = () => {
    let num = parseInt(document.querySelector("#iptNum").value);
    // console.log(num);
    if (num > 1) {
      document.querySelector("#iptNum").value =
        parseInt(document.querySelector("#iptNum").value) - 1;
      // console.log(num);
    }
    // console.log(num);
    priceSum.innerHTML = parseInt(
      document.querySelector("#iptNum").value * price
    ).toFixed(2);
  };
  document.querySelector("#add").onclick = () => {
    let num = parseInt(document.querySelector("#iptNum").value) + 1;
    // console.log(num);
    document.querySelector("#iptNum").value =
      parseInt(document.querySelector("#iptNum").value) + 1;
    priceSum.innerHTML = parseInt(num * price).toFixed(2);
  };
  // priceSum.innerHTML = num * price;
}
shopdetail(pid);
//给购物车中添加商品
async function addShop() {
  let shopNum = document.querySelector("#iptNum").value;
  // console.log(shopNum);
  let addShopCarAPI = "http://jx.xuzhixiang.top/ap/api/add-product.php";
  let uid = localStorage.getItem("uid");
  let params = {
    pnum: shopNum,
    pid: pid,
    uid: uid,
  };
  // console.log(params);
  let { data } = await axios.get(addShopCarAPI, { params });
  console.log(data);
  if (data.code == 0) {
    alert("请先登录");
    location.href = "login_page.html";
  } else {
    alert("加入购物车成功");
  }
}
//倒计时
var timer = setInterval(function () {
  countdown("2021-10-10 22:00:00", ".time");
}, 1000);
